package com.zzmcc.sys.controller;


import com.jfinal.ext.kit.PoiKit;
import com.jfinal.ext.render.excel.JxlsRender;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.zzmcc.base.BaseController;
import util.CastUtil;
import util.StringUtil;


import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DecodeController extends BaseController {
	public void index() {

		render("../decode.html");
	}
	public void down(){
		String path =PathKit.getWebRootPath() + "/tpl_decode.xls";
		String templateFileName = PathKit.getRootClassPath() + "/tpl_decode.xls";

		renderFile(new File(templateFileName));
	}
	public void export() throws ParseException {

		String path = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
		UploadFile file = getFile();

		String fileName = null;

		if (file !=null) {
			File source = file.getFile();
			fileName = file.getFileName();

			try {
				FileInputStream fis = new FileInputStream(source);
				File targetDir = new File(PathKit.getWebRootPath()  + "/u/" + path);
				if (!targetDir.exists()) {
					targetDir.mkdirs();
				}
				File target = new File(targetDir, fileName);
				if (!target.exists()) {
					target.createNewFile();
				}
				FileOutputStream fos = new FileOutputStream(target);
				byte[] bts = new byte[300];
				while (fis.read(bts, 0, 300) != -1) {
					fos.write(bts, 0, 300);
				}
				fos.close();
				fis.close();

				source.delete();


			} catch (FileNotFoundException e) {

				renderDWZErrorJson("上传出现错误，请稍后再上传");
			} catch (IOException e) {

				renderDWZErrorJson("文件写入服务器出现错误，请稍后再上传");
			}
		}
		String templateFileName = PathKit.getWebRootPath()  + "/u/" + path +"/" +fileName;
		List<Record> records  = new ArrayList<Record>();
		Object[][] dataArr1 = PoiKit.ReadExcel(templateFileName, "原始数据");
		for(int i=1;i<dataArr1.length;i++){

			Object[] rowArr1 = dataArr1[i];

			double decodeMonth= CastUtil.castDouble(rowArr1[16])+1;
			double start= CastUtil.castDouble(rowArr1[8]);
			double end= CastUtil.castDouble(rowArr1[9]);
			double avg=(end-start)/decodeMonth;
			for(int j=0;j<decodeMonth;j++){
				end=start+avg;
				Record record = new Record();
				record.set("a", rowArr1[0]);
				record.set("b", rowArr1[1]);
				record.set("c", rowArr1[2]);
				record.set("d", rowArr1[3]);
				record.set("e", rowArr1[4]);
				record.set("f", rowArr1[5]);
				record.set("g", rowArr1[6]);
				record.set("h", rowArr1[7]);
				record.set("i",start);
				record.set("j",end);
				record.set("k", CastUtil.castDouble(rowArr1[10])/decodeMonth);
				record.set("l", CastUtil.castDouble(rowArr1[11])/decodeMonth);
				record.set("m", CastUtil.castDouble(rowArr1[12])/decodeMonth);
				record.set("n", rowArr1[13]);
				record.set("o", rowArr1[14]);
				record.set("p", rowArr1[15]);
				records.add(record);
				start=end;
			}
		}

		Map<String, Object> beans = new HashMap<String, Object>();
		beans.put("records", records);
		String filename = "decode.xls";
		render(JxlsRender.me(templateFileName).filename(filename).beans(beans));


	}


	/**
	 * 计算两个日期之间相差的天数
	 * @param smdate 较小的时间
	 * @param bdate  较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate,Date bdate) throws ParseException
	{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		smdate=sdf.parse(sdf.format(smdate));
		bdate=sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days=(time2-time1)/(1000*3600*24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 *字符串的日期格式的计算
	 */
	public static int daysBetween(String smdate,String bdate) throws ParseException {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days=(time2-time1)/(1000*3600*24);

		return Integer.parseInt(String.valueOf(between_days));
	}
}
